﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Promotion : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                hdnShopID.Value = Session["SHOP_ID"].ToString();
                BindData();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }

    private void BindData()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_Deal_SELECTBYSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = hdnShopID.Value;

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            grdPromotion.AllowPaging = true;
            grdPromotion.DataSource = dt;
            grdPromotion.DataBind();

            grdPromotion.Visible = true;
        }
        else
        {
            grdPromotion.Visible = false;
            lblMessage.Visible = true;
            lblMessage.Text = "กรุณากรอกข้อมูลโปรโมชั่น";
            BindDataLocation();
        }
    }

    private void BindDataLocation()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_Location_SELECTBYSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = hdnShopID.Value;

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            btnAdd.Enabled = true;
        }
        else
        {
            btnAdd.Enabled = false;
            lblMessage.Visible = true;
            lblMessage.Text = "กรุณากรอกข้อมูลสาขาหรือข้อมูลร้าน";
        }
    }

    private int idxRow;
    protected void grdPromotion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //Binding.GridViewRowDataBound()
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblNo = (Label)e.Row.FindControl("lblNo");
                lblNo.Text = idxRow.ToString();

                Label lblID = (Label)e.Row.FindControl("lblID");

                ImageButton btnPromotionView = (ImageButton)e.Row.FindControl("btnPromotionView");
                btnPromotionView.PostBackUrl = "PromotionDetail.aspx?ShopID=" + hdnShopID.Value + "&action=view&DealID=" + lblID.Text;

                ImageButton btnPromotionEdit = (ImageButton)e.Row.FindControl("btnPromotionEdit");
                btnPromotionEdit.PostBackUrl = "PromotionDetail.aspx?ShopID=" + hdnShopID.Value + "&action=edit&DealID=" + lblID.Text;

                ImageButton btnPromotionDelete = (ImageButton)e.Row.FindControl("btnPromotionDelete");
                btnPromotionDelete.Attributes.Add("onclick", "return confirm('ยืนยันการลบข้อมูล?');");
            }
            idxRow++;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void grdPromotion_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Del")
            {
                string strKey = e.CommandArgument.ToString();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();

                SqlCommand oCommand = new SqlCommand("sp_Deal_Deal_DELETE", oDBConn.DBConnection);
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.Parameters.Add("@Deal_ID", SqlDbType.BigInt).Value = strKey;
                Int32 intPromotion = oCommand.ExecuteNonQuery();

                oDBConn.Disconnect();

                lblMessage.Text = "<br>ลบข้อมูลเรียบร้อยแล้ว<br>";
                BindData();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("PromotionDetail.aspx?ShopID=" + hdnShopID.Value + "&action=add");
    }

}
